package customerEnter.Frame.JDBCDao.SqlProcessing.SqlServer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import customerEnter.Frame.DaoDataTransmission.DaoFramUpdateOrAddMap;
import customerEnter.Frame.DaoDataTransmission.DaoFrameDMLBaseTransmission;
import customerEnter.Frame.DaoDataTransmission.DaoFramePameTransmission;
import customerEnter.Frame.DaoDataTransmission.SqlPrecompileParameter;
import customerEnter.Frame.Enum.T_SQLConditionEnum;
import customerEnter.Frame.JDBCDao.Interface.DaoFrameSqlProcessingInterface;
import customerEnter.Frame.JDBCDao.Interface.SqlPrecompileInterface;
import customerEnter.Frame.JDBCDao.Interface.SqlPrecompileParameterInterface;
import customerEnter.Frame.JDBCDao.util.DaoFrameConntroller;
import customerEnter.Frame.Utils.Log4j;
import customerEnter.Frame.Utils.javaReflexClass;
import customerEnter.tool.WhetherToEmpty;

/**
 * 类描述：處理 修改的語句
 * 
 * 类名称：DaoFrameSqlProcessingInsertImpl
 * 
 * 创建人：dy
 * 
 * 创建时间：2017-7-12 下午3:50:39
 * 
 */
public class DaoFrameSqlProcessingUpdateImpl implements DaoFrameSqlProcessingInterface<String> {

	@Override
	public <T, K> String SqlProcessing(DaoFrameDMLBaseTransmission daoFrameDMLBaseTransmission,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) {
		Object[] objects = null;
		try {
			if (daoFrameDMLBaseTransmission.isIsPreparedStatementParameter()) {
				objects = daoFrameDMLBaseTransmission.getObjects();
				daoFramePameTransmission.SQL = DaoFrameConntroller.SqlManage(daoFrameDMLBaseTransmission.getDataBase(),
						daoFrameDMLBaseTransmission.getDML(), "", "", daoFrameDMLBaseTransmission.getTableName(),
						T_SQLConditionEnum.Where.getValue() + daoFrameDMLBaseTransmission.getWhere());
			} else {
				objects = setUpdateMappingSql(daoFrameDMLBaseTransmission.getDataBase(),
						daoFrameDMLBaseTransmission.getTableName(), daoFrameDMLBaseTransmission.getMappingObject(),
						daoFrameDMLBaseTransmission.getWhere(), daoFramePameTransmission);
			}
			daoFramePameTransmission.setSqlPrecompileParameterInterface(InitlaztationObjects(objects,
					new SqlPrecompileParameter()));
		} catch (Exception e) {
			Log4j.setLog4j(getClass());
			Log4j.Log4JOutError(e.getMessage());
		}
		return daoFramePameTransmission.SQL;
	}

	/**
	 * 修改數據的方法
	 * 
	 * @param <T>
	 * @param <K>
	 * 
	 * @throws ReflectiveOperationException
	 * @throws IllegalArgumentException
	 */
	public <T, K> Object[] setUpdateMappingSql(String DataBase, String TableName, Object t, String where,
			DaoFramePameTransmission<T, K> daoFramePameTransmission) throws Exception {
		String string = "";// 獲取全部屬性的類
		DaoFramUpdateOrAddMap daoFramUpdateOrAddMap = javaReflexClass.getObjectNamesAndValuesUpdate(t);
		List<Object> list = new ArrayList<Object>();
		for (Entry<String, Object> entry : daoFramUpdateOrAddMap.getData().entrySet()) {
			if (!WhetherToEmpty.StringWhetherToEmpty(string)) {
				string += ",";
			}
			string += entry.getKey() + "=?";
			list.add(entry.getValue());
		}
		daoFramePameTransmission.SQL = DaoFrameConntroller.SqlManage(DataBase, " update ", TableName, " set ", string,
				" where 1=1  " + where);
		return list.toArray();
	}

	public SqlPrecompileParameterInterface InitlaztationObjects(Object[] objects,
			SqlPrecompileInterface sqlPrecompileInterface) {
		for (int i = 0; i < objects.length; i++) {
			sqlPrecompileInterface.AddNumberValue(i + 1, objects[i]);
		}
		return (SqlPrecompileParameterInterface) sqlPrecompileInterface;
	}

}
